import { observable, action, toJS } from 'mobx';
import * as sendSmsApi from '../../apis/sendSms';
import * as smsTempApi from '../../apis/smsTemp';
import moment from 'moment';

export default class SendSmsStore {
	@observable loading = false;
	@observable showFlag = false;
	urlParams = {};
	@observable sendHrm = [];
	@observable sendCrm = [];
	@observable regularChk = 0;
	@observable regularDateTime = moment().format('YYYY-MM-DD HH:mm');
	@observable sendStatus = '';
	@observable receiverType = 0;
	@observable msg = '';
	@observable receiverNumber = '';
	@observable messageStatus = '';
	@observable hasRight = '';
	showReply = false;
	isGov = false;
	showCusSign = false;
	@observable groups = [];
	crmIds = '';
	requestid = 0;
	@observable msgCount = 0;
	@action setLoading = (bol) => {
		this.loading = bol;
	}
	@action setGroup = (data) => {
		this.groups = data;
	}

	@action
	setUrlParams(params = {}) {
		this.urlParams = params;
	}

	@action
	getBaseData(params, callback) {
		params = { ...params, ...this.urlParams };
		sendSmsApi.getBaseData(params).then((data) => {
			const { canSend, isGov, showReply, sendHrm, sendCrm, msg, customSmsContent, showCusSign, requestid, regularDateTime, receiverType, sendStatus } = data;
			if (canSend) {
				this.hasRight = '1';
				this.isGov = isGov;
				this.showReply = showReply;
				this.sendHrm = sendHrm;
				this.sendCrm = sendCrm;
				this.regularDateTime = regularDateTime || moment().format('YYYY-MM-DD HH:mm');
				this.receiverType = receiverType;
				this.sendStatus = sendStatus;
				this.receiverNumber = data.receiverNumber;
				this.messageStatus = data.messageStatus;
				this.msg = data.customSmsContent || '';// 人员详细信息中的msg
				let crmid = [];
				if (sendCrm) {
					for (let index in sendCrm) {
						if (sendCrm[index].id) {
							crmid.push(sendCrm[index].id);
						}
					}
				}
				this.crmIds = crmid.toString();
				this.showCusSign = showCusSign;
				this.requestid = requestid;
				if (msg) {
					this.msg = msg;
					this.msgCount = msg.length;
				} else if (customSmsContent) {
					this.msg = customSmsContent;
					this.msgCount = customSmsContent.length;
				} else {
					this.msgCount = 0;
				}
			} else {
				this.hasRight = '0';
			}
			if (callback) {
				callback();
			}
		});
	}

	@action
	send(params) {
		return sendSmsApi.send(params);
	}

	@action
	setCrm(crmIds, data) {
		this.crmIds = crmIds;
		this.sendCrm = data;
	}

	@action
	setMsg(msg) {
		this.msg = msg;
		this.msgCount = msg.length;
	}

	setRegularChk(regularChk) {
		this.regularChk = regularChk;
	}

	setRegularDateTime(regularDateTime) {
		this.regularDateTime = regularDateTime;
	}

	setReceiverNumber(regularDateTime) {
		this.receiverNumber = regularDateTime;
	}

	@action
	quickSaveTemp(params) {
		return smsTempApi.quickSaveTemp(params).then((data) => {
			if (data) {
				this.groups = data;
			}
		});
	}

	@action
	getOptions(params) {
		smsTempApi.getOptions(params).then((data) => {
			this.groups = data;
			// this.showFlag=false;
		});
	}

	@action
	openTempList() {
		this.showFlag = true;
	}

	@action
	closeTempList() {
		this.showFlag = false;
	}


	clearStatus() {
		this.showFlag = false;
		this.urlParams = {};
		this.sendHrm = [];
		this.sendCrm = [];
		this.msg = '';
		this.groups = [];
		this.crmIds = [];
		this.msgCount = 0;
		this.requestid = 0;
		this.regularChk = 0;
		this.regularDateTime = moment().format('YYYY-MM-DD HH:mm');
	}
}
